const path = require('path');
const express = require('express');
const app = express();
const setCORS = require('./middleware/setCORS')
// 导入路由
const loginRouter = require('./router/loginRouter')
const userRouter = require('./router/userRouter')

// 添加超级管理员中间件
const addAdmin = require('./middleware/addAdmin')
// 引入checkLogin中间件，检查token
const checkLogin = require('./middleware/checkLogin')
/**
 * 设置中间件
 * 1. 跨域
 * 2. 静态目录
 * 3. post  data   req.body收到 
 * 4. 路由中间件
 * 
 */
// 跨域
app.use(setCORS);
// 设置静态目录
app.use(express.static(path.resolve(__dirname, './public')))
// post data
app.use(express.urlencoded({extended:false}));
app.use(express.json());

addAdmin();
// 使用路由
app.use('/admin', loginRouter);
app.use('/admin',checkLogin,userRouter);
// app.use('/admin',userRouter);

app.listen(9999, ()=>{
    console.log('server run at 9999');
})